package com.pangolin.pangolinim.ui.activity;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.PopupWindow;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.orhanobut.logger.Logger;
import com.pangolin.pangolinim.R;
import com.pangolin.pangolinim.ui.adapter.UIContact;
import com.pangolin.pangolinim.ui.base.BaseActivity;
import com.pangolin.pangolinim.utils.BundleUtil;
import com.pangolin.pangolinim.utils.FileUtil;
import com.pangolin.pangolinim.utils.GsonUtil;
import com.pangolin.pangolinim.utils.PopupWindowUtil;
import com.pangolin.pangolinim.utils.UIUtil;

import java.io.File;

public class ShowBigImageActivity extends BaseActivity {
    private ImageButton mIbToolbarMore;
    private PhotoView mPv;
    private ProgressBar mPb;

    private FrameLayout mView;
    private PopupWindow mPopupWindow;

    private UIContact me;
    private File file;
    private String filename;

    @Override
    protected void init(){
        me = GsonUtil.fromJson(getIntent().getStringExtra("param1") , UIContact.class);
        filename = getIntent().getStringExtra("param2");
        file = new File(getExternalFilesDir(String.valueOf(me.getUid())) , filename);
    }

    @Override
    protected void initView(){
        mIbToolbarMore = findViewById(R.id.ibToolbarMore);
        mPv = findViewById(R.id.pv);
        mPb = findViewById(R.id.pb);

        setToolbarTitle(filename);
        mIbToolbarMore.setVisibility(View.VISIBLE);
        mPv.enable();// 启用图片缩放功能
        Glide.with(this).load(file).placeholder(R.mipmap.default_img).centerCrop().into(mPv);
    }

    @Override
    protected void initListener(){
        mIbToolbarMore.setOnClickListener(v -> showPopupMenu());
    }

    @Override
    protected void CoreMessage(BundleUtil bundleUtil){

    }

    @Override
    protected int provideContentViewId(){
        return R.layout.activity_show_big_image;
    }

    private void showPopupMenu() {
        if (mView == null) {
            mView = new FrameLayout(this);
            mView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            mView.setBackgroundColor(UIUtil.getColor(R.color.white));

            TextView tv = new TextView(this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, UIUtil.dip2Px(45));
            tv.setLayoutParams(params);
            tv.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
            tv.setPadding(UIUtil.dip2Px(20), 0, 0, 0);
            tv.setTextColor(UIUtil.getColor(R.color.gray0));
            tv.setTextSize(14);
            tv.setText(UIUtil.getString(R.string.save_to_phone));
            mView.addView(tv);

            tv.setOnClickListener(v -> {
                if (file.exists()) {
                    try {
                        FileUtil.copyPrivateToGallery(this, file);
                        UIUtil.showToast("保存成功");
                    }catch(Exception e){
                        UIUtil.showToast(e.getMessage());
                    }
                }
                mPopupWindow.dismiss();
                mPopupWindow = null;
            });
        }
        mPopupWindow = PopupWindowUtil.getPopupWindowAtLocation(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, getWindow().getDecorView().getRootView(), Gravity.BOTTOM, 0, 0);
        mPopupWindow.setOnDismissListener(() -> PopupWindowUtil.makeWindowLight(ShowBigImageActivity.this));
        PopupWindowUtil.makeWindowDark(ShowBigImageActivity.this);
    }
}